﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using Pufang.Execl.BusinessCompute.XMLUtilities;
using System.Collections;

namespace Pufang.Execl.BusinessCompute
{
    public partial class FormConfigure : Form
    {
        string path = "";
        public FormConfigure()
        {
            InitializeComponent();
              txtPassWord.PasswordChar='*';
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog openFile = new SaveFileDialog();
            openFile.Title = "保存配置";
            openFile.Filter = "配置文件(*.pufang)|*.pufang";
            openFile.FileName = (path == null ? Application.UserAppDataPath : path);

            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                System.Collections.Hashtable hs = new System.Collections.Hashtable();
                //是否存在非空
                bool isNotNull = false;
                int Onindex = 0;
                foreach (DataGridViewRow row in grdConfigureDate.Rows)
                {
                    if (row.IsNewRow == false)
                    {
                        if (hs.ContainsKey(row.Cells[0].Value) == true)
                        {
                            MessageBox.Show("第" + (row.Index) + "行列坐标已经存在,请重新录入。");
                          
                            return;
                        }

                        if (string.IsNullOrEmpty(row.Cells[0].Value + "") == true)
                        {
                            MessageBox.Show("第" + (row.Index) + "行列坐标为空,请重新录入。");
                           
                            return;
                        }
                        if ((row.Cells[1].Value + string.Empty) == true.ToString())
                        {
                            isNotNull = true;
                        }
                        if (Onindex != 0 && GetExeclColnumName(row.Cells[0].Value + string.Empty) != Onindex)
                        {

                            MessageBox.Show("第" + (row.Index) + "行记录上一条记录所在Excel列不同。同一数据源”列头“应该在同一行。");
                          
                            return;
                        }
                        hs.Add(row.Cells[0].Value, row.Cells[1].Value);
                    }

                }

                if (isNotNull != true)
                {
                    MessageBox.Show("必须要有一个列”非“空,请重新录入。");
                    
                    return;
                }

                try
                {
                    if (string.IsNullOrEmpty(txtPassWord.Text) == false)
                    {

                        hs.Add("PassWord",txtPassWord.Text.Trim());
                    }
                    string jsonStr = Json.Serialize(hs);
                    RSACryption rsa = new RSACryption();
                    string kstrin = rsa.RSAEncrypt(rsa.xmlPublicKey, jsonStr);


                    FileOperate.WriteFile(openFile.FileName, kstrin);
                }
                catch ( Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
            }
        }

        /// <summary>
        /// 获取列Index
        /// </summary>
        /// <returns></returns>
        private int GetExeclColnumName(string cellName)
        {
            //字母位数
            int count = 0;
            foreach (char cha in cellName)
            {

                if (Convert.ToInt32(cha) >= 48 && Convert.ToInt32(cha) <= 59)
                {
                    break;
                }
                count++;
            }


            //所在行
            int colunmIndex = Convert.ToInt32(cellName.Remove(0, count));
            return colunmIndex;
 
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
         
            openFile.Title = "加载配置";
            openFile.Filter = "配置文件(*.pufang)|*.pufang";
            openFile.FileOk += new CancelEventHandler(openFile_FileOk);
            openFile.ShowDialog();
          
        }

        void openFile_FileOk(object sender, CancelEventArgs e)
        {
           string path =  (sender as OpenFileDialog).FileName;
         string fileDate =   FileOperate.ReadFile(path);
            

            RSACryption rsa = new RSACryption();
            string str = rsa.RSADecrypt(rsa.xmlkeys, fileDate);
          Hashtable  ht= Json.Deserialize<System.Collections.Hashtable>(str);

          grdConfigureDate.Rows.Clear();
          foreach (string key in ht.Keys)
          {
             if(key=="PassWord"){
                 txtPassWord.PasswordChar = new char();
                  txtPassWord.Text =ht[key]+string.Empty;
                 continue;
             }
              int count = grdConfigureDate.Rows.Add();
              grdConfigureDate.Rows[count].Cells[0].Value = key;
              grdConfigureDate.Rows[count].Cells[1].Value = ht[key];
          }
        }

     
    }
}
